﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace quanlysinhvien
{
    public partial class FormAdd_Subject : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataReader rd;
        public FormAdd_Subject()
        {
            InitializeComponent();
        }
        private bool checkexistsubcod()
        {
            bool check = false;
            string subcod = txtSubCode.Text;
            conn = Connect.getConnection();
            conn.Open();
            string sql = "select * from TblSubject where Subject_Code ='" + subcod + "'";
            cmd = new SqlCommand(sql, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                check = true;
            }
            da.Dispose();
            conn.Close();
            return check;
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtSubCode.Text == "" || txtSubName.Text == "" || txtTerm.Text == "" || txtAbb.Text == "" || txtNoC.Text =="")
            {
                MessageBox.Show("Please insert");
            }
            else
            {
                if (checkexistsubcod())
                {
                    MessageBox.Show("Subject Code is existed!");
                    txtSubCode.SelectAll();
                    txtSubCode.Focus();
                }
                else
                {
                    conn = Connect.getConnection();
                    try
                    {
                        if (conn != null)
                        {
                            conn.Open();
                            String strsql = "insert into TblSubject values ('" + txtSubCode.Text + "',N'" + txtSubName.Text + "','" + txtTerm.Text + "',N'" + txtAbb.Text + "','" + txtNoC.Text + "')";
                            cmd = new SqlCommand(strsql, conn);
                            rd = cmd.ExecuteReader();
                            conn.Close();
                            MessageBox.Show("Insert Successful!");
                            txtAbb.Text="";
                            txtNoC.Text = "";
                            txtSubCode.Text = "";
                            txtSubName.Text = "";
                            txtTerm.Text = "";
                            txtSubCode.Focus();

                        }
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Error", "Please try again");
                    }
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            FormMain formMain = new FormMain();
            formMain.Show();
            this.Hide();
            //this.Dispose();
        }
       

    }
}
